package com.sky.controller.user;

import com.sky.dto.UserLoginDTO;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController("userController")
@RequestMapping("/user/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    //登录，微信登陆
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录：{}",userLoginDTO);

        //调用service进行登录
        UserLoginVO userLoginVO =userService.wxLogin(userLoginDTO);
        //生成jwt令牌
        HashMap<String, Object> claims = new HashMap<>();
        //设置jwt的body
        claims.put("userId",userLoginVO.getId());
        //生成jwt令牌
        String jwt = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        //将jwt令牌存放到userLoginVO中
        userLoginVO.setToken(jwt);
        log.info("jwt令牌：{}",userLoginVO);
        return Result.success(userLoginVO);
    }




}
